<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 添加/编辑银行卡 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 表单区 -->
		<view class="container-form">
			<view class="form-item flex align-items-center">
				<view class="item-label">姓名</view>
				<view class="item-input flex-item">
					<input class="input" v-model="formData.name" type="text" placeholder="请输入姓名" placeholder-class="placeholder" />
				</view>
			</view>
			<view class="form-item flex align-items-center">
				<view class="item-label">银行名称</view>
				<view class="item-input flex-item">
					<input class="input" v-model="formData.bank_name" type="text" placeholder="请输入银行名称" placeholder-class="placeholder" />
				</view>
			</view>
			<view class="form-item flex align-items-center">
				<view class="item-label">银行卡号</view>
				<view class="item-input flex-item">
					<input class="input" v-model="formData.bank_card_number" type="number" placeholder="请输入银行卡号" placeholder-class="placeholder" />
				</view>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-btn" @click="handleSubmit()">{{bankCardId ? "编辑" : "添加"}}</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 银行卡id
				bankCardId: null,
				// 表单内容
				formData: {},
				// 延时器
				timeout: null,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			if (option && option.id) {
				this.bankCardId = option.id
				this.$nextTick(() => {
					uni.setNavigationBarTitle({
						title: "编辑银行卡"
					})
				})
				uni.showLoading({
					title: "加载中"
				})
				this.getBankCardDetails(() => {
					uni.hideLoading()
					this.loadEnd = true
				})
			} else {
				this.$nextTick(() => {
					uni.setNavigationBarTitle({
						title: "添加银行卡"
					})
				})
				this.loadEnd = true
			}
		},
		onUnload() {
			clearTimeout(this.timeout)
		},
		methods: {
			// 获取银行卡详情
			getBankCardDetails(fn) {
				this.$util.request("main.bankCard.details", {
					id: this.bankCardId
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.formData = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取银行卡详情 ', error)
				})
			},
			// 添加/编辑
			handleSubmit() {
				if (!this.formData.name) {
					uni.showToast({
						icon: "none",
						title: "请输入姓名",
					})
					return
				}
				if (!this.formData.bank_name) {
					uni.showToast({
						icon: "none",
						title: "请输入银行名称",
					})
					return
				}
				if (!this.formData.bank_card_number) {
					uni.showToast({
						icon: "none",
						title: "请输入银行卡号",
					})
					return
				}
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var url = ""
				if (this.bankCardId) url = "main.bankCard.edit"
				else url = "main.bankCard.add"
				this.$util.request(url, this.formData).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						if (this.formData.id) {
							uni.showToast({
								icon: "success",
								title: "编辑成功",
								mask: true,
								duration: 1500
							})
						} else {
							uni.showToast({
								icon: "success",
								title: "添加成功",
								mask: true,
								duration: 1500
							})
						}
						this.timeout = setTimeout(() => {
							uni.navigateBack()
						}, 1500);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('添加/编辑银行卡 ', error)
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx 32rpx 144rpx;

		.container-form {
			border-radius: 32rpx;
			background: #FFF;
			padding: 0 32rpx;

			.form-item {
				border-top: 1px solid rgba(0, 0, 0, 0.10);

				&:first-child {
					border-top: none;
				}

				.item-label {
					color: #242629;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
				}

				.item-input {
					margin-left: 24rpx;

					.input {
						width: 100%;
						color: #242629;
						text-align: right;
						font-size: 28rpx;
						line-height: 40rpx;
						padding: 40rpx 0;
					}

					.placeholder {
						color: #8D929C;
					}
				}
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;
			padding: 16rpx 24rpx;

			.footer-btn {
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				background: var(--theme-color);
				text-align: center;
			}
		}
	}
</style>